package models;

import java.util.Date;
import java.util.List;

import siena.Column;
import siena.Entity;
import siena.Id;
import siena.Model;
import siena.Query;

@Entity
public class User extends Model {

	@Id
	long id;

	@Column("first_name")
	String firstName;

	@Column("last_name")
	String lastName;

	@Column("mail")
	String mail;

	@Column("phone")
	String phone;

	@Column("deviceRegKey")
	String deviceRegKey;

	public User(String firstName, String lastName, String mail, String phone,
			String deviceRegKey) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.mail = mail;
		this.phone = phone;
		this.deviceRegKey = deviceRegKey;
	}

	public String toString() {
		return "[" + this.id + "]" + this.firstName + " " + this.lastName + " "
				+ this.mail + " " + this.phone + " " + this.deviceRegKey;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public long getId() {
		return id;
	}

	/**
	 * @author loai
	 */
	public static Query<User> all() {
		return Model.all(User.class);
	}

	/**
	 * @author loai
	 */
	public static User findByPhone(String phone) {
		return all().filter("phone", phone).get();
	}

	/**
	 * @author loai
	 */
	public UserLocation getLocation() {
		return UserLocation.all().filter("user", this).get();
	}

	/**
	 * @author loai
	 */
	public List<UserPermission> getUsersPermissions() {
		return UserPermission.all().filter("sender", this).fetch();
	}

	/**
	 * @author loai
	 */
	public Date getLastSeen() {
		return getLocation().getLastSeen();
	}

	public String getDeviceRegKey() {
		return deviceRegKey;
	}

	public void setDeviceRegKey(String deviceRegKey) {
		this.deviceRegKey = deviceRegKey;
	}

	/**
	 * Returns the user whose id is id
	 * 
	 * @author Aya Kamel
	 * @param id
	 *            : User id
	 * @return User Object
	 */
	public static User getUser(long id) {
		return (all(User.class).filter("id", id)).get();
	}

	/**
	 * Resets the Users table
	 * 
	 * @author Aya Kamel
	 */
	public static void resetTable() {
		List<User> users = all(User.class).fetch();
		for (User u : users) {
			u.delete();
		}
	}

	/**
	 * Returns a string form of the User table
	 * 
	 * @author Aya Kamel
	 * @return
	 */
	public static String showTable() {
		String data = "";
		List<User> users = all(User.class).fetch();
		for (User u : users) {
			data += u + "\n";
		}
		return data;
	}

	// public static User findByID(long id)
	// {
	// return ((all(User.class)).filter("id", id)).get();
	// }

	/**
	 * the user with complete data
	 * 
	 * @return User with complete data
	 * @author Gad
	 */
	public User fetchData() {
		return getUser(getId());
	}

}
